package work20241226;

/**
 * @author zdl
 * @date 2024/12/26
 */
class Shop{
    private String name;
    private double price;
    private boolean isProduct;

    public Shop() {
    }

    public Shop(String name, double price, boolean isProduct) {
        this.name = name;
        this.price = price;
        this.isProduct = isProduct;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return price
     */
    public double getPrice() {
        return price;
    }

    /**
     * 设置
     * @param price
     */
    public void setPrice(double price) {
        this.price = price;
    }

    /**
     * 获取
     * @return isProduct
     */
    public boolean isIsProduct() {
        return isProduct;
    }

    /**
     * 设置
     * @param isProduct
     */
    public void setIsProduct(boolean isProduct) {
        this.isProduct = isProduct;
    }

    public String toString() {
        return "Shop{name = " + name + ", price = " + price + ", isProduct = " + isProduct + "}";
    }
}
//消费者
class Consumer implements Runnable{
    private Shop shop;

    public Consumer(Shop shop){
        this.shop = shop;
    }
    @Override
    public void run() {
        while (true){
            synchronized (shop){
                if (shop.isIsProduct()){//true  需要生产，此时消费者等待
                    try {
                        shop.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else { //false 不需要生产，直接购买
                    //消费者购买完后  此时生产者没有商品了 需要唤醒生产者 并将isProduct 设置为true
                    shop.notify();
                    shop.setIsProduct(true);
                    System.out.println("消费者购买:"+shop.getName()+",价格为:"+shop.getPrice());

                }
            }
        }
    }
}
//生产者
class Producer implements Runnable{
    private Shop shop;

    public Producer(Shop shop){
        this.shop = shop;
    }

    @Override
    public void run() {
        int count = 0;
        while (true){
            synchronized (shop){
                if (shop.isIsProduct()){//true 需要生产
                    if (count % 2 == 0){
                        shop.setName("冰淇淋");
                        shop.setPrice(2);
                    }else {
                        shop.setName("棒棒糖");
                        shop.setPrice(1);
                    }
                    count++;
                    //商品生产完后，需要唤醒消费者   并把 isProduct设置为 false
                    shop.setIsProduct(false);
                    System.out.println("生产者生产了:"+shop.getName()+",价格为:"+shop.getPrice());
                    shop.notify();
                }else {//false 不需要生产  等待即可
                    try {
                        shop.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}
public class Demo13 {
    public static void main(String[] args) {
        //13. 【生产者消费者问题】编写一个程序，使用同步代码块解决生产者-消费者问题。
        Shop shop = new Shop("辣条",2,false);
        Consumer consumer = new Consumer(shop);
        Producer producer = new Producer(shop);
        new Thread(consumer,"消费者线程").start();
        new Thread(producer,"生产者线程").start();
    }
}
